package com.letao.dao;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import com.letao.pojo.Goods;
import com.letao.pojo.GoodsImage;

/**
 * A data access object (DAO) providing persistence and search support for
 * GoodsImage entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.letao.pojo.GoodsImage
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class GoodsImageDAO {
	private static final Logger log = LoggerFactory
			.getLogger(GoodsImageDAO.class);
	// property constants
	public static final String GIMAGE = "gimage";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(GoodsImage transientInstance) {
		log.debug("saving GoodsImage instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	private byte[] fileToByte(File file) throws IOException {  
		InputStream in = new FileInputStream(file);
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();  
		int ch;  
		while ((ch = in.read()) != -1) {  
		bytestream.write(ch);  
		}  
		byte imgdata[] = bytestream.toByteArray();  
		bytestream.close();  

		return imgdata;  
		}  
	public void save(Goods goods,List<File> fileList){
		log.debug("saving GoodsImage instance");
		try {
			for(File file:fileList){
				GoodsImage transientInstance=new GoodsImage();
				transientInstance.setGimage(Hibernate.getLobCreator(getCurrentSession()).createBlob(fileToByte(file)));
				transientInstance.setGoods(goods);
				save(transientInstance);
			}
			log.debug("save successful");
		} catch (Exception re) {
			log.error("save failed", re);
			//throw re;
		}
	}
	public void delete(GoodsImage persistentInstance) {
		log.debug("deleting GoodsImage instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GoodsImage findById(java.lang.Integer id) {
		log.debug("getting GoodsImage instance with id: " + id);
		try {
			GoodsImage instance = (GoodsImage) getCurrentSession().get(
					"com.letao.pojo.GoodsImage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GoodsImage instance) {
		log.debug("finding GoodsImage instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("com.letao.pojo.GoodsImage")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GoodsImage instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GoodsImage as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByGimage(Object gimage) {
		return findByProperty(GIMAGE, gimage);
	}

	public List findAll() {
		log.debug("finding all GoodsImage instances");
		try {
			String queryString = "from GoodsImage";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GoodsImage merge(GoodsImage detachedInstance) {
		log.debug("merging GoodsImage instance");
		try {
			GoodsImage result = (GoodsImage) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GoodsImage instance) {
		log.debug("attaching dirty GoodsImage instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GoodsImage instance) {
		log.debug("attaching clean GoodsImage instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GoodsImageDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GoodsImageDAO) ctx.getBean("GoodsImageDAO");
	}
}